package com.cy.dda.web.admin;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cy.dda.comm.constant.Constants;
import com.cy.dda.comm.utils.CommonUtils;
import com.cy.dda.model.Result;
import com.cy.dda.model.ao.rule.ManagerLoginAO;
import com.cy.dda.model.enums.MessageType;
import com.cy.dda.model.vo.rule.MenuVO;
import com.cy.dda.service.message.MessageService;
import com.cy.dda.service.rule.ManagerService;
import com.cy.dda.service.rule.RuleService;
import com.cy.dda.service.statistics.MaintainStatisticsService;
import com.cy.dda.web.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 后台-公用方法
 *
 * @author: tanyilin
 * @create: 2018年10月26日 下午2:38:28
 */
@Slf4j
@RestController
@RequestMapping("/admin")
@Api(tags = "后台-公用方法")
public class AdminHomeController extends BaseController{

	@Autowired
	private ManagerService managerService;
	@Autowired
	private RuleService ruleService;
	@Autowired
	private MaintainStatisticsService maintainStatisticsService;
	@Autowired
	private MessageService messageService;
	
	@ApiOperation("管理员登录")
	@PostMapping("/login")
	public Result login(@Validated ManagerLoginAO managerLoginAO, BindingResult bindingResult) {
		
		this.checkError(bindingResult);
		Result result = managerService.login(managerLoginAO.getMobile(), managerLoginAO.getPassword());
		return result;
	}
	
	@ApiOperation("已登录的管理员的左侧菜单栏")
    @GetMapping("/menus")
    public Result<List<MenuVO>> menus() {

        Long roleId = this.getManager().getRoleId();
        List<MenuVO> menus = ruleService.selectLeftMenusInCahce(roleId);

        return ok(null, menus);
    }
	
	@ApiOperation("维护统计催收记录1")
	@PostMapping("/statistics/collection/record/one")
	public Result maintainStatisticsCollectionRecord(@RequestParam @DateTimeFormat(pattern = Constants.yyyy_MM_dd_HH_mm_ss) Date date) {
		maintainStatisticsService.maintainStatisticsCollectionRecord(date);
		return ok();
	}
	
	@ApiOperation("维护统计催收记录2")
	@PostMapping("/statistics/collection/record/two")
	public Result maintainStatisticsCollectionRecord(@RequestParam @DateTimeFormat(pattern = Constants.yyyy_MM_dd_HH_mm_ss) Date startDate, 
			@RequestParam @DateTimeFormat(pattern = Constants.yyyy_MM_dd_HH_mm_ss) Date endDate) {
		
		List<Date> list = new ArrayList<Date>();
		Date date = startDate;
		while(date.compareTo(endDate) <= 0) {
			list.add(date);
			date = CommonUtils.addTime(date, 1, Calendar.DAY_OF_YEAR);
		}
		
		for(Date date2 : list) {
			maintainStatisticsService.maintainStatisticsCollectionRecord(date2);
		}
		
		return ok();
	}
	
	@ApiOperation("维护统计回款记录1")
	@PostMapping("/statistics/repay/record/one")
	public Result maintainStatisticsRepayRecord(@RequestParam @DateTimeFormat(pattern = Constants.yyyy_MM_dd_HH_mm_ss) Date date) {
		maintainStatisticsService.maintainStatisticsRepayRecord(date);
		return ok();
	}
	
	@ApiOperation("维护统计回款记录2")
	@PostMapping("/statistics/repay/record/two")
	public Result maintainStatisticsRepayRecord(@RequestParam @DateTimeFormat(pattern = Constants.yyyy_MM_dd_HH_mm_ss) Date startDate, 
			@RequestParam @DateTimeFormat(pattern = Constants.yyyy_MM_dd_HH_mm_ss) Date endDate) {
		
		List<Date> list = new ArrayList<Date>();
		Date date = startDate;
		while(date.compareTo(endDate) <= 0) {
			list.add(date);
			date = CommonUtils.addTime(date, 1, Calendar.DAY_OF_YEAR);
		}
		
		for(Date date2 : list) {
			maintainStatisticsService.maintainStatisticsRepayRecord(date2);
		}
		return ok();
	}
	
	@ApiOperation("测试发送微信消息")
	@PostMapping("/test/send/wechat/message")
	public Result sendWechatMessage(@RequestParam Long casesId) {
		messageService.sendMessage(casesId.toString(), MessageType.CASES_STATUS_NOTIFICATION);
		return ok();
	}
	
}
